package com.ld.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ld.pojo.Dept;
import com.ld.pojo.Emp;
import com.ld.service.DeptService;
import com.ld.service.EmpService;
import com.ld.utils.PageResult;
import com.ld.vo.EmpVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-08-15-15:00
 * @Description:
 */
@Controller
@RequestMapping("/emp2")
public class EmpController2 {
    @Reference
    private EmpService empService;
    @Reference
    private DeptService deptService;

    @RequestMapping("list")
    public String findAll(Model model){
        //得到学生列表
        List<Emp> emps = empService.findAll();
        //将得到的学生列表放到model中
        model.addAttribute("emps",emps);
        //得到班级列表并放到model中
        List<Dept> depts = deptService.list();
        model.addAttribute("depts",depts);
        //返回逻辑视图名
        return "/emp/list";
    }

    @RequestMapping("search")
    public String search(@RequestParam(defaultValue = "1",required = false) Integer page,
                         @RequestParam(defaultValue = "6",required = false) Integer pagesize,
                         EmpVo vo, Model model,
                         HttpServletRequest request, HttpServletResponse response, HttpSession session){
        //得到查询结果
        PageResult<Emp> emps = empService.search(page, pagesize, vo);
        //将emps放到model
        model.addAttribute("emps",emps);
        //将vo放到model
        model.addAttribute("vo",vo);
        //将depts放到model
        model.addAttribute("depts",deptService.list());
        return "/emp/list";
    }

    //添加学生
    @RequestMapping("add")
    public String add(Emp emp){
        //添加到数据库中
        empService.add(emp);
//        System.out.println("emp = " + emp);
        return "redirect:/emp2/search.do";
    }

    //修改学生
    @RequestMapping("update")
    public String update(Emp emp){
        //修改到数据库中
        empService.update(emp);
//        System.out.println("emp = " + emp);
        return "redirect:/emp2/search.do";
    }

    @RequestMapping("delete")
    public String delete(Integer eid){
        empService.delete(eid);
        return "redirect:/emp2/search.do";
    }

}
